<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\agency\controller;

use app\agency\service\NodeService;
use library\Controller;
use library\tools\Data;
use think\Console;
use think\Db;
use think\exception\HttpResponseException;
use app\common\facade\TransItem;
use app\common\facade\Arr;
use think\Request;
use app\common\services\MarketPublishService;

/**
 * 二手管理
 * Class Index
 * @package app\agency\controller
 */
class Market extends AgentBase
{
    public $table = 'Market';

    /**
     * 产品概况
     * @auth true
     * @menu true
     */
    public function index()
    {   
        //获取当前代理商所在城市
        $user=$this->agentUser;
        $data['provinces_id']=$user['provinces_id'];
        $data['city_id']=$user['city_id'];

       
        $result['total_market']=DB::name($this->table)->where($data)->count();
        $result['sucess_market']=DB::name($this->table)->where($data)->where('status=3 ')->count();
        $result['pre_market']=DB::name($this->table)->where($data)->where('status=2')->count();
        $result['deny_market']=DB::name($this->table)->where($data)->where('status=4')->count();


        $this->assign('result',$result);
        $this->fetch();
    }

    /**
    * 添加商品
    * @auth true
    *@menu true
    */
    public function marketAdd()
    {   

        if ($this->request->isGet()) {
           
            $this->title = '添加二手';
            //获取当前代理商所在城市
            $user=$this->agentUser;

            //商品类型
            $marktype = Db::name('MarketType')
                    ->where('status=1')
                    ->field("id,name")
                    ->select();
            $this->assign('marktype',$marktype);

            //获取代理商所在省份城市
            $address['province']=DB::name("Area")->where('area_id',$user['provinces_id'])->value('area_name');
            $address['city']=DB::name("Area")->where('area_id',$user['city_id'])->value('area_name');
            $this->assign('address',$address);

            //获取代理商所在所有区域
            $county=DB::name("Area")->where('area_parent_id',$user['city_id'])->field('area_id,area_name')->select();
            $this->assign('county',$county);

            $this->fetch();
        }else{

            //获取当前代理商所在城市
            $user=$this->agentUser;

            $data=$this->request->post();
            $county=DB::name("Area")->where('area_id',$data['county_id'])->value('area_name');
            $data1['name']=$data['name'];
            $data1['tid']=$data['tid'];
            $data1['oprice']=$data['oprice'];
            $data1['money']=$data['money'];
            $data1['contacts']=$data['contacts'];
            $data1['mobile']=$data['mobile'];
            $data1['address']=$data['address'];
            $data1['is_show']=$data['is_show'];
            $data1['top']=$data['top'];
            $data1['depreciation']=$data['depreciation'];
            $data1['wechat_num']=$data['wechat_num'];
            $data1['qq_num']=$data['qq_num'];
            $data1['logo']=$data['logo'];
            $data1['image']=$data['image'];
            $data1['extendContent']=$data['content'];
            $data1['county_id']=$data['county_id'];
            $data1['provinces_name']=$data['province']." ".$data['city']." ".$county;
            $data1['provinces_id']=$user['provinces_id'];
            $data1['city_id']=$user['city_id'];
            $data1['created_at']=time();
            $data1['updated_at']=time();
            $data1['status']=3;//后台代理商发布不用审核
            $data1['aid']=$user['id'];
            //$result=DB::name($this->table)->insert($data1);

            $MarketPublishService = new MarketPublishService();
            $result=$MarketPublishService->publishMarketGoods($data1);
            if($result){
                $this->success('商品添加成功','javascript:history.back()');
            }else{
                $this->error('商品添加失败');
            }
            
        }
       
    }

     /**
    * 编辑商品
    * @auth true
    */
    public function edit(){

        
        if ($this->request->isGet()) {
           
            $this->title = '编辑二手';
            //获取当前代理商所在城市
            $user=$this->agentUser;

            //商品类型
            $marktype = Db::name('MarketType')
                    ->where('status=1')
                    ->field("id,name")
                    ->select();
            $this->assign('marktype',$marktype);

            //获取代理商所在省份城市
            $address['province']=DB::name("Area")->where('area_id',$user['provinces_id'])->value('area_name');
            $address['city']=DB::name("Area")->where('area_id',$user['city_id'])->value('area_name');
            $this->assign('address',$address);

            //获取代理商所在所有区域
            $county=DB::name("Area")->where('area_parent_id',$user['city_id'])->field('area_id,area_name')->select();
            $this->assign('county',$county);

            $id = $this->request->param('id', 0);
            $result=DB::name($this->table)
                    ->alias('m')
                    ->join('MarketExtend me','m.id=me.market_id')
                    ->where('m.id',$id)
                    ->field("m.*,me.content,me.id as markid")
                    ->find();

            $this->assign('result',$result);

            $this->fetch();
        }else{
            //获取当前代理商所在城市
            $user=$this->agentUser;

            $data=$this->request->post();
            $county=DB::name("Area")->where('area_id',$data['county_id'])->value('area_name');
            $data1['name']=$data['name'];
            $data1['tid']=$data['tid'];
            $data1['oprice']=$data['oprice'];
            $data1['money']=$data['money'];
            $data1['contacts']=$data['contacts'];
            $data1['mobile']=$data['mobile'];
            $data1['address']=$data['address'];
            $data1['is_show']=$data['is_show'];
            $data1['top']=$data['top'];
            $data1['depreciation']=$data['depreciation'];
            $data1['wechat_num']=$data['wechat_num'];
            $data1['qq_num']=$data['qq_num'];
            $data1['logo']=$data['logo'];
            $data1['image']=$data['image'];
            $data1['extendContent']=$data['content'];
            $data1['county_id']=$data['county_id'];
            $data1['provinces_name']=$data['province']." ".$data['city']." ".$county;
            $data1['provinces_id']=$user['provinces_id'];
            $data1['city_id']=$user['city_id'];
            $data1['updated_at']=time();
            $data1['status']=3;//后台代理商发布不用审核
            $data1['aid']=$user['id'];
            $data1['id']=$data['id'];
            $data1['extendId']=$data['mid'];
            //$result=DB::name($this->table)->update($data1);
            $MarketPublishService = new MarketPublishService();
            $result=$MarketPublishService->publishMarketGoods($data1);
            if($result){
                $this->success('商品编辑成功','javascript:history.back()');
            }else{
                $this->error('商品编辑失败');
            }
        }
       
    }


  

    /**
    * 产品列表
    * @auth true
    *@menu true
    */
    public function marketList()
    {   

        $this->title = '商品列表';

        $status = $this->request->param('status',-1);
        
        if($status=='-1'){
            $data[]=['status','IN',[2,3,4]];
        }else{
            $data[]=['status','EQ',$status];
            if($status==2){
                $this->title = '待审核商品';
            }elseif ($status==3) {
                $this->title = '审核通过商品';
            }elseif ($status==4) {
                $this->title = '审核拒绝商品';
            }else{
                $data[]=['status','IN',[2,3,4]];
            }
        }
        $result=$this->agentUser;
        $data[]=['provinces_id','EQ',$result['provinces_id']];
        $data[]=['city_id','EQ',$result['city_id']];

        $markettype=DB::name("MarketType")->field("id,name")->select();
        $this->assign('markettype',$markettype);

        $query = $this->_query($this->table)
            ->where($data)
            ->like('name')
            ->equal('id,name,tid,status,is_show');
        $query->order('id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _marketList_page_filter(&$data)
    {

        $this->clist = Db::name('MarketType')->field("id,name")->select();
        foreach ($data as &$vo) {

            $vo['marketstatus'] = TransItem::marketStatus(Arr::get($vo, 'status'));
            $vo['province'] = DB::name("Area")->where('area_id',$vo['provinces_id'])->value("area_name");
            $vo['city'] = DB::name("Area")->where('area_id',$vo['city_id'])->value("area_name");
            $vo['add_time'] = date("Y-m-d",$vo['created_at']);
            foreach ($this->clist as $cate) if ($cate['id'] === $vo['tid']) $vo['cate'] = $cate;

        }
    }

    /**
       * 商品下架
       * @auth true
     */
     public function forbid()
    {
        $this->_save($this->table, ['is_show' => '0']);
    }

    /**
       * 商品上架
       * @auth true
     */
    public function resume()
    {
        $this->_save($this->table, ['is_show' => '1']);
    }

    //商品置顶
    public function top(){
        $top = $this->request->param('top');
        $this->_save($this->table, ['top' => $top]);
    }


     /**
       * 商品审核
       * @auth true
     */
    public function auditMarket(){

        $data=$this->request->post();

        if($data['status']==3){
            //审核通过
            //$result = DB::name($this->table)->update($data);
            $MarketPublishService = new MarketPublishService();
            $result=$MarketPublishService->approvedGoods($data['id'],0);
            if($result){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }else{

            if(empty($data['replay'])){
                $this->error('请输入拒绝理由');
            }else{
                //$result = $result = DB::name($this->table)->update($data);
                $MarketPublishService = new MarketPublishService();
                $result=$MarketPublishService->rejectGoods($data['id'],0,$data['replay']);
                if($result){
                    $this->success('操作成功');
                }else{
                    $this->error('操作失败');
                }
            }
        }


    }

     public function deny(){
        $this->title = '审核兼职';
        $this->assign('id',$this->request->param('id', 0));
        $this->fetch('deny');
    }





    



}
